const formatTime = (timestamp, type) => {
  const date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  let str = '';
  if (type == 1) {
    str = [year, month, day].map(formatNumber).join('-') + '  ' + [hour, minute, second].map(formatNumber).join(':');
  } else if (type == 2) {
    str = month + '月' + day + '日' + '  ' + [hour, minute].map(formatNumber).join(':');
  } else if (type == 3) {
    str = [year, month, day].map(formatNumber).join('-') + '  ' + [hour, minute].map(formatNumber).join(':');
  }
  return str
}
const formatStr = (str) => {
  let tempArr = str.split('&');
  return tempArr.join("\n");
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const parseGoldNum = (num) => {
  let sign = "";
  if (num < 0) {
    sign = "-";
  }
  num = Math.abs(num);
  let newNum = "";
  if (num < 10000) {
    newNum = num;
  } else if (num >= 10000 && num < 100000) {
    newNum = Math.floor(num / 10) / 1000 + "万";
  } else if (num >= 100000 && num < 1000000) {
    newNum = Math.floor(num / 100) / 100 + "万";
  } else if (num >= 1000000 && num < 10000000) {
    newNum = Math.floor(num / 1000) / 10 + "万";
  } else if (num >= 10000000 && num < 100000000) {
    newNum = Math.floor(num / 10000) + "万";
  } else if (num >= 100000000 && num < 1000000000) {
    newNum = Math.floor(num / 100000) / 1000 + "亿";
  } else if (num >= 1000000000 && num < 10000000000) {
    newNum = Math.floor(num / 1000000) / 100 + "亿";
  } else if (num >= 10000000000 && num < 100000000000) {
    newNum = Math.floor(num / 10000000) / 10 + "亿";
  } else if (num >= 100000000000 && num < 1000000000000) {
    newNum = Math.floor(num / 100000000) + "亿";
  } else if (num >= 1000000000000 && num < 10000000000000) {
    newNum = Math.floor(num / 1000000000) / 1000 + "万亿";
  } else if (num >= 10000000000000 && num < 100000000000000) {
    newNum = Math.floor(num / 10000000000) / 100 + "万亿";
  } else if (num >= 100000000000000 && num < 1000000000000000) {
    newNum = Math.floor(num / 100000000000) / 10 + "万亿";
  } else if (num >= 1000000000000000 && num < 10000000000000000) {
    newNum = Math.floor(num / 1000000000000) + "万亿";
  } else {
    newNum = Math.floor(num / 10000000000000000) + "兆";
  }
  return sign + newNum;
}

//生成请求头
const getRequestHeader = () => {
  let date = new Date();
  let version = 251;
  let time = date.getFullYear().toString() + (date.getMonth() + 1).toString() + date.getDate().toString() + date.getHours().toString() + date.getMinutes().toString();
  let token = md5(md5('c1c9214c1b7565c90903dcb959401c89' + version + time));
  let requestHeader = {
    'version': version,
    'deviceidfa': '7F14FA23-1C97-4E53-9B81-D4C22B770421',
    'token': token,
    'time': time
  };
  return requestHeader;
};

module.exports = {
  formatTime: formatTime,
  requestHeader: getRequestHeader,
  parseGoldNum: parseGoldNum,
  formatStr: formatStr
}